# -*- coding: utf-8 -*-

def frange(start, end, step=1.0):
    if float(step) == 0.0: return []
    r = []
    c = 0
    while True:
        next = start + c*step
        if step > 0 and next >= end:
            break
        elif step < 0 and next <= end:
            break
        r.append(next)
        c += 1
    return r

def hex2rgb(h):
    h = h.replace("#","")
    r = int(h[0:2],16)
    g = int(h[2:4],16)
    b = int(h[4:6],16)
    return r,g,b

def rgb2hex(rgb):
    l = []
    for x in rgb: l.append(int(x))
    return '#'+"%02x"%l[0]+"%02x"%l[1]+"%02x"%l[2]

def gradient(i, f, steps):
    if type(i) not in [tuple,list]:
       i = hex2rgb(i)
    if type(f) not in [tuple,list]:
       f = hex2rgb(f)
    r1,g1,b1 = i
    r2,g2,b2 = f
    rstep = (r2-r1)/(steps-1)
    gstep = (g2-g1)/(steps-1)
    bstep = (b2-b1)/(steps-1)
    R = frange(r1,r2+rstep,rstep)
    G = frange(g1,g2+gstep,gstep)
    B = frange(b1,b2+bstep,bstep)

    l = []
    for i in range(len(R)):
      l.append([round(R[i]),round(G[i]),round(B[i])])

    return l
